﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ViewModel;

namespace IssueTracker.CustomControls
{
    public partial class search_result : UserControl
    {
        public issueList iList;
        public int displayCount { get; set; }
        ViewModel.Filter doFilter = null;
        string term = "";
        Issue currentItem = null;
        public search_result(string term, ViewModel.Filter doFilter)
        {
            InitializeComponent();
            this.term = term;
            this.doFilter = doFilter;
        }

        private void SearchResult_Load(object sender, EventArgs e)
        {
            List<Issue> result = Service.Issues.Search(term, doFilter);

            List<Issue> issuesOpen = result.Where(x => x.Status == Status.New || x.Status == Status.Open).ToList();
            List<Issue> issuesInProgress = result.Where(x => x.Status == Status.InProgress).ToList();
            List<Issue> issuesClosed = result.Where(x => x.Status == Status.Closed).ToList();
            List<Issue> issuesRejected = result.Where(x => x.Status == Status.Rejected).ToList();

            //Service.Issues.GetAllIssues(ref issuesOpen, ref issuesInProgress, ref issuesClosed, ref issuesRejected, 50);
            ListViewGroup groupOpen = new ListViewGroup("Open Issues");
            ListViewGroup groupInProgress = new ListViewGroup("In Progress Issues");
            ListViewGroup groupClosed = new ListViewGroup("Closed Issues");
            ListViewGroup groupRejected = new ListViewGroup("Rejected Issues");


            listViewResult.Groups.Add(groupOpen);
            listViewResult.Groups.Add(groupInProgress);
            listViewResult.Groups.Add(groupClosed);
            listViewResult.Groups.Add(groupRejected);

            listViewResult.ContextMenuStrip = contextMenuStrip1;
            foreach (var item in issuesOpen)
            {
                ListViewItem i = new ListViewItem(groupOpen);
                i.Text = item.DisplaySearchText;
                i.Tag = item;
                i.ImageIndex = (int)item.Priority;
                listViewResult.Items.Add(i);
            }

            foreach (var item in issuesInProgress)
            {
                ListViewItem i = new ListViewItem(groupInProgress);
                i.Text = item.DisplaySearchText;
                i.Tag = item;
                i.ImageIndex = (int)item.Priority;
                listViewResult.Items.Add(i);
            }

            foreach (var item in issuesClosed)
            {
                ListViewItem i = new ListViewItem(groupClosed);
                i.Text = item.DisplaySearchText;
                i.Tag = item;
                i.ImageIndex = (int)item.Priority;
                listViewResult.Items.Add(i);
            }

            foreach (var item in issuesRejected)
            {
                ListViewItem i = new ListViewItem(groupRejected);
                i.Text = item.DisplaySearchText;
                i.Tag = item;
                i.ImageIndex = (int)item.Priority;
                listViewResult.Items.Add(i);
            }
            displayCount = issuesOpen.Count + issuesInProgress.Count + issuesClosed.Count + issuesRejected.Count;
            iList.SetCountValue(displayCount);

            //save result for export
            iList.result = result;
        }

        private void listViewResult_MouseDown(object sender, MouseEventArgs e)
        {
            // ListView lv = (ListView)sender;

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            iList.ShowIssueDetailsForm(currentItem);
        }

        private void listViewResult_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewResult.SelectedItems.Count > 0)
            {
                var item = listViewResult.SelectedItems[0];
                currentItem = (Issue)item.Tag;
            }
        }

        private void listViewResult_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            iList.ShowIssueDetailsForm(currentItem);
        }
    }
}
